package com.suray.wcs.web.system.controller;

import com.suray.basic.wcs.utils.Coord;
import com.suray.commin.annotation.Log;
import com.suray.commin.core.controller.BaseController;
import com.suray.commin.core.controller.ResponseData;
import com.suray.commin.core.page.TableDataInfo;
import com.suray.commin.core.utils.SecurityUtils;
import com.suray.commin.enmus.BusinessType;
import com.suray.wcs.service.system.entity.ConnectConfDB;
import com.suray.wcs.service.system.entity.NodeDB;
import com.suray.wcs.service.system.service.impl.ConnectConfServiceImpl;
import com.suray.wcs.service.system.service.impl.NodeDBServiceImpl;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @Description:任务接驳点配置前端控制器
 * @author: chenhw
 * @date:2022年05月9日 上午9:55:14
 */
@RestController
@RequestMapping("/connectConf")
@CrossOrigin
public class ConnectConfDBController extends BaseController {
    @Autowired
    private ConnectConfServiceImpl connectConfService;

    @Autowired
    private NodeDBServiceImpl nodeDBService;

    private Logger log = LoggerFactory.getLogger(ConnectConfDBController.class);

    /**
     * 查询接驳点配置
     *
     * @param connectConfDB
     * @return
     */
    @PreAuthorize("@ss.hasPermi('connectConf:list')")
    @ApiOperation("查询接驳点配置")
    @GetMapping(value = "/list")
    public TableDataInfo queryConnectConf(ConnectConfDB connectConfDB) {
        startPage();
        List<ConnectConfDB> result = connectConfService.queryConnectConf(connectConfDB);

        return getDataTable(result);
    }

    /**
     * 获取接驳点配置
     */
    @ApiOperation("获取接驳点配置")
    @GetMapping(value = "/{connectId}")
    public ResponseData getInfo(@PathVariable Long connectId) {
        ConnectConfDB connectConfDB = connectConfService.selectByConnectId(connectId);

        if (connectConfDB.getPreConnect() != null){
            connectConfDB.setPreConnects(connectConfDB.getPreConnect().split(";"));
        }
        if (connectConfDB.getSufConnect() != null){
            connectConfDB.setSufConnects(connectConfDB.getSufConnect().split(";"));
        }
        return ResponseData.success(connectConfDB);
    }

    /**
     * 新增配置
     *
     * @param connectConfDB
     * @return
     */
    @PreAuthorize("@ss.hasPermi('connectConf:add')")
    @Log(title = "新增接驳点配置", method = "新增接驳点配置", businessType = BusinessType.INSERT)
    @ApiOperation("新增接驳点配置")
    @PostMapping(value = "/addConnectConf")
    public ResponseData addProgramme(@Validated @RequestBody ConnectConfDB connectConfDB) {
        connectConfDB.setCreateBy(SecurityUtils.getUsername());

        if (connectConfDB.getPreConnects() != null) {
            StringBuffer coords = new StringBuffer();
            for (int i = 0; i < connectConfDB.getPreConnects().length; i++) {
                String connect = connectConfDB.getPreConnects()[i];
                coords.append(connect + ";");
            }
            connectConfDB.setPreConnect(coords.toString());
        }
        if (connectConfDB.getSufConnects() != null) {
            StringBuffer coords = new StringBuffer();
            for (int i = 0; i < connectConfDB.getSufConnects().length; i++) {
                String connect = connectConfDB.getSufConnects()[i];
                coords.append(connect + ";");
            }
            connectConfDB.setSufConnect(coords.toString());
        }
        connectConfDB.setCreateTime(new Date());
        if (connectConfService.save(connectConfDB)) {
            return ResponseData.success();
        } else {
            return ResponseData.error("新增接驳点配置失败");
        }
    }

    /**
     * 接驳点配置删除
     */
    @PreAuthorize("@ss.hasPermi('connectConf:remove')")
    @ApiOperation("接驳点配置删除")
    @Log(title = "接驳点配置删除", method = "接驳点配置删除", businessType = BusinessType.DELETE)
    @DeleteMapping(value = "delConnectConf/{connectIds}")
    public ResponseData getInfo(@PathVariable Long[] connectIds) {
        if (connectConfService.removeByIds(Arrays.asList(connectIds))) {
            return ResponseData.success();
        } else {
            return ResponseData.error("接驳点配置删除失败");
        }
    }

    /**
     * 修改接驳点配置
     */
    @PreAuthorize("@ss.hasPermi('connectConf:edit')")
    @Log(title = "修改接驳点配置", method = "修改接驳点配置", businessType = BusinessType.UPDATE)
    @ApiOperation("修改接驳点配置")
    @PutMapping("/updateConnectConf")
    public ResponseData updateConnectConf(@Validated @RequestBody ConnectConfDB connectConfDB) {
        connectConfDB.setUpdateBy(SecurityUtils.getUsername());

        if (connectConfDB.getPreConnects() != null) {
            StringBuffer coords = new StringBuffer();
            for (int i = 0; i < connectConfDB.getPreConnects().length; i++) {
                String connect = connectConfDB.getPreConnects()[i];
                coords.append(connect + ";");
            }
            connectConfDB.setPreConnect(coords.toString());
        }
        if (connectConfDB.getSufConnects() != null) {
            StringBuffer coords = new StringBuffer();
            for (int i = 0; i < connectConfDB.getSufConnects().length; i++) {
                String connect = connectConfDB.getSufConnects()[i];
                coords.append(connect + ";");
            }
            connectConfDB.setSufConnect(coords.toString());
        }
        boolean result = connectConfService.updateById(connectConfDB);

        return toAjax(result);
    }

    /**
     * 获取前置接驳点
     *
     * @return
     */
    @ApiOperation("获取接驳点")
    @GetMapping("/connectCoords")
    public ResponseData getConnectCoord() {
        List<Coord> coordList = new ArrayList<>();
        for (NodeDB portCrood : nodeDBService.getNodeDBByType()) {
            coordList.add(new Coord(portCrood.getGridx(), portCrood.getGridy(), portCrood.getGridz()));
        }
        return ResponseData.success(coordList);
    }
}
